const app = getApp()
export default {
  props: {
    errMsg: {
      type: String,
      default: '当前无数据'
    }
  },
  data: function () {
    return {
      title: 'picker-view',
      value: [0, 0],
      list: [], // 一级列表
      secondList: [], // 二级列表
      visible: true,
      indicatorStyle: `height: 34px;`
    }
  },
  watch: {
  },
  methods: {
    togglePopup(isShow, payload) {
      if (isShow) {
        const { arr = [], value = [] } = payload;
        this.list = arr;
        if (value.length > 0) {
          // 查找第一层的角标
          const idx = this.list.findIndex(i => i.val == value[0]);
          // 查找第二层的角标
          const jobIdx = this.list[idx].children.findIndex(i => i.val == value[1]);
          // 两层角标拼成数组  回显给选择器默认
          this.value = [idx, jobIdx == -1 ? 0 : jobIdx];
        }
      }
      this.$refs.basicPopup.togglePopup(isShow)
    },
    handleConfirm() {
      if (!this.list[this.value[0]].children.length) {
        return uni.showToast({ title: '部门没有岗位，请联系BOSS添加', icon: 'none' })
      }
      const params = {
        id: this.list[this.value[0]].key,
        name: this.list[this.value[0]].val,
        childId: this.list[this.value[0]].children[this.value[1]].key,
        childName: this.list[this.value[0]].children[this.value[1]].val,
      }
      this.$emit('onConfirm', params)
      this.togglePopup(false);
    },
    handleCancel() {
      this.togglePopup(false);
    },
    bindChange(e) {
      console.log('---------------->', e);
      const val = e.detail.value;
      this.value = val;
    }
  }
}